package com.yoomiito.app.ui.anyview.gasstation

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.model.GasFormat
import com.yoomiito.app.model.ViewType
import com.yoomiito.app.ui.yijiayou.GasFormatAdapter
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.L
import com.yoomiito.app.widget.SortTabItemView

class GasSortTabView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    companion object {
        const val DefaultOilNum = 92
        const val DistanceSort = -2
        const val PriceSort = -3

    }

    private val data1 = mutableListOf<ViewType<GasFormat>>().also {
        it.add(ViewType<GasFormat>(GasFormatAdapter.Subtitle, GasFormat("汽油", -1, false)))
        it.add(ViewType<GasFormat>(GasFormatAdapter.Content, GasFormat("92#", 92, true)))
        it.add(ViewType<GasFormat>(GasFormatAdapter.Content, GasFormat("95#", 95, false)))
        it.add(ViewType<GasFormat>(GasFormatAdapter.Content, GasFormat("98#", 98, false)))
        it.add(ViewType<GasFormat>(GasFormatAdapter.Subtitle, GasFormat("柴油", -1, false)))
        it.add(ViewType<GasFormat>(GasFormatAdapter.Content, GasFormat("0#", 0, false)))
//        it.add(ViewType<GasFormat>(GasFormatAdapter.Content, GasFormat("-40#", -40, false)))
    }
    private val data2 = mutableListOf<ViewType<GasFormat>>().also {
        it.add(ViewType<GasFormat>(GasFormatAdapter.Content, GasFormat("距离优先", DistanceSort, true)))
        it.add(ViewType<GasFormat>(GasFormatAdapter.Content, GasFormat("价格优先", PriceSort, false)))
    }
    private var sortView1: SortTabItemView
    private var sortView2: SortTabItemView
    private var subtitle: TextView
    var onResultListener: (Int) -> Unit = {}

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_gas_sort_tab, null, false).also {
            subtitle = it.findViewById(R.id.subtitle)
            sortView2 = it.findViewById<SortTabItemView>(R.id.type2Sort).also { sort ->
                sort.onFormatClickListener = { gasFormat ->
                    onResultListener(gasFormat.nameNum)
                }
            }
            sortView1 = it.findViewById<SortTabItemView>(R.id.type1Sort).also { sort ->
                sort.onFormatClickListener = { gasFormat ->
                    onResultListener(gasFormat.nameNum)
//                    data2.forEachIndexed { index, viewType ->
//                        viewType.t.select = index == 0
//                    }
//                    sortView2.mText = "距离优先"
                }

            }

        }
        val lp = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.dp2px(48f))
        addView(view, lp)
    }

    fun init(shadeView: View, subtitleShow: Boolean = true) {
        subtitle.visibility = if (subtitleShow) View.VISIBLE else View.GONE

        sortView1.data = data1
        sortView1.shadeView = shadeView
        sortView1.parentView = this

        sortView2.data = data2
        sortView2.shadeView = shadeView
        sortView2.parentView = this
    }

    fun reset() {
        data1.forEach {
            if (DefaultOilNum == it.t.nameNum){
                sortView1.resetInit(it.t.name)
            }
            it.t.select = DefaultOilNum == it.t.nameNum
        }
        data2.forEachIndexed { index, viewType ->
            if (index == 0){
                sortView2.resetInit(viewType.t.name)
            }
            viewType.t.select = index == 0
        }

    }

}